package com.cater.sys.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.UserContext;
import com.cater.query.CommonQuery;
import com.cater.sys.entity.Area;
import com.cater.sys.entity.Dep;
import com.cater.sys.entity.User;
import com.cater.sys.service.DepService;
import com.cater.sys.service.UserService;
import com.cater.sys.service.impl.AreaServiceImpl;
import com.cater.sys.service.impl.DictionarydataServiceImpl;
import com.cater.utils.TreeObject;
import com.cater.utils.TreeUtil;

/**
 * 
 * @描述:首页
 * @作者 :ShenDong
 * @创建日期 :2016年12月7日8:41
 *
 */
@Controller
@RequestMapping("/dep")
public class DepController {

	@Autowired
	DepService depService;

	@Autowired
	UserService userService;

	@Autowired
	private AreaServiceImpl areaService;

	@Autowired
	DictionarydataServiceImpl service;

	/**
	 * 打开部门
	 * 
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String login_default(Model model) {
		model.addAttribute("service", areaService);
		return "sys/dep/dep_list";
	}

	@ResponseBody
	@RequestMapping(value = "/getAll")
	@SystemControllerLog(description = "查看部门")
	public List<Dep> getAll(HttpServletRequest request) {
		Integer depid = UserContext.getdeppId();
		List<Dep> list = null;
		if (UserContext.isadmin()) {
			list = depService.getAll();
		} else {
			list = depService.getAllbypid(depid);
		}

		for (Dep dep : list) {
			dep.setName(dep.getDepName());
			dep.setOpen("true");
		}
		return list;
	}

	@ResponseBody
	@RequestMapping(value = "/getdepBypid")
	@SystemControllerLog(description = "根据部门ID查询部门")
	public JSONObject getdepBypid(Integer nodeid, CommonQuery query) {
		JSONObject json = new JSONObject();
		if (nodeid == null) {
			nodeid = 0;
		}
		TreeUtil treeUtil = new TreeUtil();
		TreeObject[] rows = treeUtil.getTreeObjects(depService.getdepBypid(nodeid, query));
		json.put("rows", rows);
		return json;
	}

	@RequestMapping("/addView")
	public String addView(Model model, CommonQuery query) {
		String level = query.getLevel();
		if (StringUtils.isNotEmpty(level)) {
			model.addAttribute("level", level);
		}
		return "sys/dep/dep_add";
	}

	@RequestMapping("/adddepView")
	public String adddepView(Model model, CommonQuery query) {
		String id = query.getId();
		if (StringUtils.isNotEmpty(id)) {
			model.addAttribute("id", id);
		}
		model.addAttribute("id", id);
		String level = query.getLevel();
		if (StringUtils.isNotEmpty(level)) {
			model.addAttribute("level", level);
		}
		return "sys/dep/dep_adddep";
	}

	@ResponseBody
	@RequestMapping("/add")
	@SystemControllerLog(description = "新增公司")
	public JSONObject add(Dep dep) {
		JSONObject json = new JSONObject();
		try {
			Integer id = depService.save(dep);
			if (id > 0) {
				if (dep.getPid() == 0) {
					dep.setCompanyId(id);
					depService.update(dep);
				} else {
					Dep pDep = depService.getByid(dep.getPid());
					if (pDep != null) {
						dep.setCompanyId(pDep.getCompanyId());
						depService.update(dep);
					}
				}
			}
			json.put("code", 200);
			json.put("message", "添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "添加失败");
		}
		return json;
	}

	@ResponseBody
	@RequestMapping("/delete")
	@SystemControllerLog(description = "删除公司或部门")
	public JSONObject delete(Integer id, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			Dep dep = depService.getByid(id);
			if (dep.getCompanyId() == 0) {
				List<Dep> deplist = depService.getdepBypid(dep.getDepId(), query);
				for (Dep dep2 : deplist) {
					List<User> users = userService.getUserbydepid(dep2.getDepId());
					for (User user : users) {
						userService.deletestate(user.getId());
					}
					depService.deletestate(dep2.getDepId());
				}
			} else {
				List<User> users = userService.getUserbydepid(dep.getDepId());
				for (User user : users) {
					userService.deletestate(user.getId());
				}
			}
			depService.deletestate(dep.getDepId());
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	@RequestMapping("/showeditdep")
	public String showeditdep(Integer id, Model model, CommonQuery query) {
		Dep dep = depService.getByid(id);
		model.addAttribute("dep", dep);
		return "sys/dep/dep_edit";
	}

	@ResponseBody
	@RequestMapping("/editdep")
	@SystemControllerLog(description = "修改公司")
	public JSONObject editdep(Dep dep) {
		JSONObject json = new JSONObject();
		try {
			depService.update(dep);
			json.put("code", 200);
			json.put("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}
}
